package com.example.zyl.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zyl.api.entity.SysDict;
import com.example.zyl.api.entity.dto.SysDictDto;
import com.example.zyl.api.entity.vo.SysDictVo;
import com.example.zyl.api.mapper.SysDictMapper;
import com.example.zyl.api.service.SysDictService;
import com.example.zyl.common.query.page.PageParam;
import com.example.zyl.common.utils.ToolUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * (SysDict)表服务实现类
 *
 * @author zyl
 * @since 2025-01-14 13:58:48
 */
@Service("sysDictService")
@RequiredArgsConstructor
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    private final SysDictMapper sysDictMapper;


    @Override
    public SysDict queryById(Long dictId) {
        SysDictVo sysDictVo = new SysDictVo();
        sysDictVo.setSex("1");
        sysDictVo.setUserType("0");
        sysDictVo.convert();
        return this.sysDictMapper.queryById(dictId);
    }

    /**
     * 分页查询
     *
     * @param pageParam 筛选条件
     * @return 查询结果
     */
    @Override
    public Page<SysDict> queryByPage(PageParam<SysDict, SysDictDto> pageParam) {
        //获取查询条件
        SysDictDto tableDto = pageParam.getParam();
        //获取分页信息
        Page<SysDict> page = pageParam.getPage();
        //构建查询条件
        QueryWrapper<SysDict> wrapper = tableDto.defaultWrapper();
        //执行查询
        return sysDictMapper.selectPage(page, wrapper);
    }

    /**
     * 新增数据
     *
     * @param sysDict 实例对象
     * @return 实例对象
     */
    @Override
    public SysDict insert(SysDict sysDict) {
        this.sysDictMapper.insert(sysDict);
        return sysDict;
    }

    /**
     * 修改数据
     *
     * @param sysDict 实例对象
     * @return 实例对象
     */
    @Override
    public SysDict update(SysDict sysDict) {
        this.sysDictMapper.update(sysDict, Wrappers.emptyWrapper());
        return this.queryById(sysDict.getDictId());
    }

    /**
     * 通过主键删除数据
     *
     * @param dictId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long dictId) {
        return this.sysDictMapper.deleteById(dictId) > 0;
    }

    @Override
    public SysDict getDictByCode(String type, String code) {
        LambdaQueryWrapper<SysDict> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ToolUtils.isNotEmpty(code), SysDict::getDictCode, code);
        queryWrapper.eq(ToolUtils.isNotEmpty(type), SysDict::getDictType, type);
        List<SysDict> dictList = this.sysDictMapper.selectList(queryWrapper);
        if (!dictList.isEmpty()) {
            return dictList.get(0);
        }
        return null;
    }
}
